{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "physical-costa",
   "metadata": {},
   "source": [
    "# French Company Identification Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "broke-carol",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "confused-pressure",
   "metadata": {},
   "source": [
    "The function `clean_fr_siren()` cleans a column containing French personal identification number (SIREN) strings, and standardizes them in a given format. The function `validate_fr_siren()` validates either a single SIREN strings, a column of SIREN strings or a DataFrame of SIREN strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "turned-intent",
   "metadata": {},
   "source": [
    "SIREN strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"552008443\"\n",
    "* `standard`: SIREN strings with proper whitespace in the proper places. Note that in the case of SIREN, the compact format is the same as the standard one.\n",
    "* `tva`: return a TVA that preposes two extra check digits to the data, like \"19 552 008 443\".\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_fr_siren()` and `validate_fr_siren()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "magnetic-backing",
   "metadata": {},
   "source": [
    "### An example dataset containing SIREN strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "prime-intensity",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"siren\": [\n",
    "            '552 008 443',\n",
    "            '404833047',\n",
    "            '7542011030',\n",
    "            '7552A10004',\n",
    "            '8019010008', \n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\",\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"1111 S Figueroa St, Los Angeles, CA 90015\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "breathing-origin",
   "metadata": {},
   "source": [
    "## 1. Default `clean_fr_siren`\n",
    "\n",
    "By default, `clean_fr_siren` will clean siren strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dress-alarm",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_fr_siren\n",
    "clean_fr_siren(df, column = \"siren\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "informational-parade",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "virgin-liberia",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fatal-summer",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "minus-object",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fr_siren(df, column = \"siren\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "heard-terminal",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "equal-basics",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fr_siren(df, column = \"siren\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "norman-river",
   "metadata": {},
   "source": [
    "### `tva`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "administrative-ottawa",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fr_siren(df, column = \"siren\", output_format=\"tva\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "intellectual-description",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned SIREN strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "descending-savings",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fr_siren(df, column=\"siren\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "divided-looking",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "alternative-supplier",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cellular-trash",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fr_siren(df, \"siren\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "exposed-organization",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "controversial-wedding",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fr_siren(df, \"siren\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "studied-reply",
   "metadata": {},
   "source": [
    "## 4. `validate_fr_siren()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "regional-school",
   "metadata": {},
   "source": [
    "`validate_fr_siren()` returns `True` when the input is a valid SIREN. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_fr_siren()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_fr_siren()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_fr_siren()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "numerical-ontario",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_fr_siren\n",
    "print(validate_fr_siren('552 008 443'))\n",
    "print(validate_fr_siren('404833047'))\n",
    "print(validate_fr_siren('7542011030'))\n",
    "print(validate_fr_siren('7552A10004'))\n",
    "print(validate_fr_siren('8019010008'))\n",
    "print(validate_fr_siren(\"hello\"))\n",
    "print(validate_fr_siren(np.nan))\n",
    "print(validate_fr_siren(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "imperial-blink",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "unlike-logan",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_fr_siren(df[\"siren\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "municipal-sullivan",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "checked-vermont",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_fr_siren(df, column=\"siren\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bound-personality",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "suspected-landscape",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_fr_siren(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "sharp-title",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
